//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "PlotPredictBindingSite.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fstream>
#include <iostream>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define MAX_SIZE                  500000
#define AUTO_SCALE                0
#define AUTO_SCALE_WEIGHTED       1
#define SHOW_ANCHOR_RESIDUES      0
#define SHOW_ANCHOR_RESIDUES_AVG  1
#define SHOW_Y_TICS               1
#define USE_IMPULSE               1
#define USE_IMPULSE_AVG           0
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPredictBindingSite::PlotPredictBindingSite () {

  this->filenameIn                            = new String ();
  this->folderOut                             = new String ();

  this->title                                 = new String ();
  this->listPredictionBS                      = new TListE <PredictBindingSiteByPattern> ();

  this->toString                              = new String ();

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPredictBindingSite::PlotPredictBindingSite (const PlotPredictBindingSite& plotPredictBindingSite) {

  this->filenameIn                            = new String (plotPredictBindingSite.filenameIn);
  this->folderOut                             = new String (plotPredictBindingSite.folderOut);

  this->title                                 = new String (plotPredictBindingSite.title);
  this->listPredictionBS                      = new TListE <PredictBindingSiteByPattern> (plotPredictBindingSite.listPredictionBS);

  this->toString                              = new String (plotPredictBindingSite.toString);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPredictBindingSite::PlotPredictBindingSite (const PlotPredictBindingSite* plotPredictBindingSite) {

  this->filenameIn                            = new String (plotPredictBindingSite->filenameIn);
  this->folderOut                             = new String (plotPredictBindingSite->folderOut);

  this->title                                 = new String (plotPredictBindingSite->title);
  this->listPredictionBS                      = new TListE <PredictBindingSiteByPattern> (plotPredictBindingSite->listPredictionBS);

  this->toString                              = new String (plotPredictBindingSite->toString);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPredictBindingSite::~PlotPredictBindingSite () {

  if (this->filenameIn)       delete this->filenameIn;
  if (this->folderOut)        delete this->folderOut;
  if (this->title)            delete this->title;
  if (this->listPredictionBS) delete this->listPredictionBS;
  if (this->toString)         delete this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PlotPredictBindingSite::Title (String* title) {

  if (title)
    *(this->title) = *title;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PlotPredictBindingSite::FolderOut (String* folderOut) {

  if (folderOut)
    *(this->folderOut) = *folderOut;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PlotPredictBindingSite::FilenameIn (String* filenameIn) {

  if (filenameIn)
    *(this->filenameIn) = *filenameIn;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PlotPredictBindingSite::ListPredictionBS (TListE <PredictBindingSiteByPattern>* listPredictionBS) {

  if (listPredictionBS)
    *(this->listPredictionBS) = *listPredictionBS;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PlotPredictBindingSite::Title (void) {

  return this->title;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PlotPredictBindingSite::FolderOut (void) {

  return this->folderOut;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PlotPredictBindingSite::FilenameIn (void) {

  return this->filenameIn;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <PredictBindingSiteByPattern>* PlotPredictBindingSite::ListPredictionBS (void) {

  return this->listPredictionBS;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPredictBindingSite::LoadData (void) {
  
  if (!this->filenameIn || !this->filenameIn->Out())
    return false;
  
  ifstream file (this->filenameIn->Out());
  
  if (!file.is_open())
    return false;
  
  char    buffer [MAX_SIZE];
  String* data = new String ("");
  
  while (!file.eof()) {
    
    file.getline(buffer, MAX_SIZE);
    
    if (buffer[0] == '>') {
      
      if (data->Length()) {
        
        PredictBindingSiteByPattern* newPredictBindingSiteByPattern = new PredictBindingSiteByPattern ();
        
        if (newPredictBindingSiteByPattern->LoadDataFromString(data))
          this->listPredictionBS->Add(newPredictBindingSiteByPattern);
        else
          delete newPredictBindingSiteByPattern;
        
      }
      
      data->In(buffer);
      data->Add("\n");
      
    }
    else {
      data->Add(buffer);
      data->Add("\n");
    }
    
  }
  
  if (data->Length()) {

    PredictBindingSiteByPattern* newPredictBindingSiteByPattern = new PredictBindingSiteByPattern ();

    if (newPredictBindingSiteByPattern->LoadDataFromString(data))
      this->listPredictionBS->Add(newPredictBindingSiteByPattern);
    else
      delete newPredictBindingSiteByPattern;

  }

  file.close();
  
  if (data) delete data;
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPredictBindingSite::PlotOneByOne (char *name = NULL) {

  if (!this->listPredictionBS || !this->listPredictionBS->Length() || 
      !this->folderOut        || !this->folderOut->Length()        ||
      !this->title            || !this->title->Length())
    return false;

  char          buffer [MAX_SIZE];
  String*       filenameOut = new String ();
  String*       cmd         = new String ();
  
  this->listPredictionBS->SetInitial();
  while (this->listPredictionBS->SetNext()) {
    
    PredictBindingSiteByPattern* currentPredictBindingSiteByPattern = this->listPredictionBS->GetCurrent();
    
    gnuplot_ctrl*                plot                               = gnuplot_init();
    
    float*                       probabilitiesAA                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesPH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesSS                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCumul                 = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       anchorResiduesPos                  = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    int*                         positions                          = new int [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
      
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++) {
      probabilitiesAA[i]    = currentPredictBindingSiteByPattern->ScoreAA()[i];
      probabilitiesCH[i]    = currentPredictBindingSiteByPattern->ScoreCH()[i];
      probabilitiesPH[i]    = currentPredictBindingSiteByPattern->ScorePH()[i];
      probabilitiesSS[i]    = currentPredictBindingSiteByPattern->ScoreSS()[i];
      probabilitiesCumul[i] = currentPredictBindingSiteByPattern->ScoreCumul()[i];
      anchorResiduesPos[i]  = currentPredictBindingSiteByPattern->AnchorResidues()[i];
    }
    
    char* parts     = strtok(currentPredictBindingSiteByPattern->Header()->Out(), "[;]");
    int   countPart = 0;
    int   counter   = 0;
    
    while (parts) {
      if (countPart++ > 0) {
        int start, end;
        sscanf(parts, "%d-%d", &start, &end);
        for (int i = start; i <= end; i++)
          if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
            positions[counter++] = i;
      }
      parts = strtok(NULL, "[;]");
    }
    
    if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
      for (int i = counter; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++, counter++) 
        positions[counter] = positions[counter - 1] + 1;
    
    filenameOut->In(this->folderOut);
    if (filenameOut->Out()[filenameOut->Length() - 1] != '/')
      filenameOut->Add("/");
    strncpy(buffer, currentPredictBindingSiteByPattern->Header()->Out() + 1, 7);
    buffer[7] = '\0';
    filenameOut->Add(buffer);
    filenameOut->Add(".Binding.Site.Prediction");
    if (name != NULL)
      filenameOut->Add(name);
    gnuplot_saveplotPNG(plot, filenameOut->Out(), 7, 600, 600);
    
    cmd->In("Ligand: ");
    cmd->Add(this->title->Out());
    cmd->Add(" - CATH Domain: ");
    cmd->Add(buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 1\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set label \"%s\" at screen 0.5,0.99 center front font \"FreeMono-Bold, 8\" tc rgb \"black\"\0", cmd->Out());
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set multiplot\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style line 1 lt -1 lc rgb \"#E69F17\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 2 lt -1 lc rgb \"#11AD34\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 3 lt -1 lc rgb \"#1D4599\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 4 lt -1 lc rgb \"#2F3F60\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 5 lt -1 lc rgb \"#666666\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 6 lt -1 lc rgb \"red\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.795\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 1":"points ls 1");
    sprintf(buffer, "Identification by amino acid patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.615\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 2":"points ls 2");
    sprintf(buffer, "Identification by chemical patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
     
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.435\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 3":"points ls 3");
    sprintf(buffer, "Identification by physical patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
        
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.250\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 4":"points ls 4");
    sprintf(buffer, "Identification by secondary structure patterns (predicted)\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.032\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set tmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set xtics 0, 10, %ld\0", currentPredictBindingSiteByPattern->Sequence()->Length());
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    cmd->In("set xtics (");
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i = i + 10) {
      sprintf(buffer, ((!i)?"'%d' %d":", '%d' %d"), positions[i], i);
      cmd->Add(buffer);
    }
    cmd->Add(") \0");
    gnuplot_cmd(plot, cmd->Out());
    sprintf(buffer, "set xtics rotate by 270 font \"FreeMono-Bold 6\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    
    gnuplot_setstyle(plot, "impulses ls 6");
    sprintf(buffer, "Anchor residues\0");
    gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_set_xlabel(plot, "Amino acid position in the sequence", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_setstyle(plot, (USE_IMPULSE_AVG)?"impulses ls 5":"points ls 5");
    sprintf(buffer, "Identification by patterns consensus\0");
    gnuplot_replot_x(plot, probabilitiesCumul, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset multiplot\0");
    gnuplot_cmd(plot, buffer);

    gnuplot_close(plot);

    if (probabilitiesAA)    delete [] probabilitiesAA;
    if (probabilitiesCH)    delete [] probabilitiesCH;
    if (probabilitiesPH)    delete [] probabilitiesPH;
    if (probabilitiesSS)    delete [] probabilitiesSS;
    if (probabilitiesCumul) delete [] probabilitiesCumul;
    if (anchorResiduesPos)  delete [] anchorResiduesPos;
    if (positions)          delete [] positions;
    
  }
  
  if (filenameOut) delete filenameOut;
  if (cmd)         delete cmd;
  
  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPredictBindingSite::PlotOneByOnePS (void) {

  if (!this->listPredictionBS || !this->listPredictionBS->Length() || 
      !this->folderOut        || !this->folderOut->Length()        ||
      !this->title            || !this->title->Length())
    return false;

  char          buffer [MAX_SIZE];
  String*       filenameOut = new String ();
  String*       cmd         = new String ();
  
  this->listPredictionBS->SetInitial();
  while (this->listPredictionBS->SetNext()) {
    
    PredictBindingSiteByPattern* currentPredictBindingSiteByPattern = this->listPredictionBS->GetCurrent();
    
    gnuplot_ctrl*                plot                               = gnuplot_init();
    
    float*                       probabilitiesAA                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesPH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesSS                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCumul                 = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       anchorResiduesPos                  = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    int*                         positions                          = new int [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
      
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++) {
      probabilitiesAA[i]    = currentPredictBindingSiteByPattern->ScoreAA()[i];
      probabilitiesCH[i]    = currentPredictBindingSiteByPattern->ScoreCH()[i];
      probabilitiesPH[i]    = currentPredictBindingSiteByPattern->ScorePH()[i];
      probabilitiesSS[i]    = currentPredictBindingSiteByPattern->ScoreSS()[i];
      probabilitiesCumul[i] = currentPredictBindingSiteByPattern->ScoreCumul()[i];
      anchorResiduesPos[i]  = currentPredictBindingSiteByPattern->AnchorResidues()[i];
    }
    
    char* parts     = strtok(currentPredictBindingSiteByPattern->Header()->Out(), "[;]");
    int   countPart = 0;
    int   counter   = 0;
    
    while (parts) {
      if (countPart++ > 0) {
        int start, end;
        sscanf(parts, "%d-%d", &start, &end);
        for (int i = start; i <= end; i++)
          if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
            positions[counter++] = i;
      }
      parts = strtok(NULL, "[;]");
    }
    
    if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
      for (int i = counter; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++, counter++) 
        positions[counter] = positions[counter - 1] + 1;
    
    filenameOut->In(this->folderOut);
    if (filenameOut->Out()[filenameOut->Length() - 1] != '/')
      filenameOut->Add("/");
    strncpy(buffer, currentPredictBindingSiteByPattern->Header()->Out() + 1, 7);
    buffer[7] = '\0';
    filenameOut->Add(buffer);
    filenameOut->Add(".Binding.Site.Prediction");
    gnuplot_saveplotPS(plot, filenameOut->Out(), 7, 600, 600);
    
    cmd->In("Ligand: ");
    cmd->Add(this->title->Out());
    cmd->Add(" - CATH Domain: ");
    cmd->Add(buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 1\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set label \"%s\" at screen 0.5,0.97 center front font \"FreeMono-Bold, 10\" tc rgb \"black\"\0", cmd->Out());
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set multiplot\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style line 1 lt -1 lc rgb \"#E69F17\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 2 lt -1 lc rgb \"#11AD34\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 3 lt -1 lc rgb \"#1D4599\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 4 lt -1 lc rgb \"#2F3F60\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 5 lt -1 lc rgb \"#666666\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 6 lt -1 lc rgb \"red\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.776\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 1":"points ls 1");
    sprintf(buffer, "Identification by amino acid patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.604\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 2":"points ls 2");
    sprintf(buffer, "Identification by chemical patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
     
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.432\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 3":"points ls 3");
    sprintf(buffer, "Identification by physical patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.260\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 4":"points ls 4");
    sprintf(buffer, "Identification by secondary structure patterns (predicted)\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.060\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set tmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set xtics 0, 10, %ld\0", currentPredictBindingSiteByPattern->Sequence()->Length());
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    cmd->In("set xtics (");
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i = i + 10) {
      sprintf(buffer, ((!i)?"'%d' %d":", '%d' %d"), positions[i], i);
      cmd->Add(buffer);
    }
    cmd->Add(") \0");
    gnuplot_cmd(plot, cmd->Out());
    sprintf(buffer, "set xtics rotate by 270 font \"FreeMono-Bold 6\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    
    gnuplot_setstyle(plot, "impulses ls 6");
    sprintf(buffer, "Anchor residues\0");
    gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_set_xlabel(plot, "Amino acid position in the sequence", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_setstyle(plot, (USE_IMPULSE_AVG)?"impulses ls 5":"points ls 5");
    sprintf(buffer, "Identification by patterns consensus\0");
    gnuplot_replot_x(plot, probabilitiesCumul, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset multiplot\0");
    gnuplot_cmd(plot, buffer);

    gnuplot_close(plot);

    if (probabilitiesAA)    delete [] probabilitiesAA;
    if (probabilitiesCH)    delete [] probabilitiesCH;
    if (probabilitiesPH)    delete [] probabilitiesPH;
    if (probabilitiesSS)    delete [] probabilitiesSS;
    if (probabilitiesCumul) delete [] probabilitiesCumul;
    if (anchorResiduesPos)  delete [] anchorResiduesPos;
    if (positions)          delete [] positions;
    
  }
  
  if (filenameOut) delete filenameOut;
  if (cmd)         delete cmd;
  
  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPredictBindingSite::PlotAllInOne (void) {

  if (!this->listPredictionBS || !this->listPredictionBS->Length() || 
      !this->folderOut        || !this->folderOut->Length()        ||
      !this->title            || !this->title->Length())
    return false;

  char          buffer [MAX_SIZE];
  String*       filenameOut = new String ();
  String*       cmd         = new String ();
  
  this->listPredictionBS->SetInitial();
  while (this->listPredictionBS->SetNext()) {
    
    PredictBindingSiteByPattern* currentPredictBindingSiteByPattern = this->listPredictionBS->GetCurrent();

    gnuplot_ctrl*                plot                               = gnuplot_init();
    
    float*                       probabilitiesAA                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesPH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesSS                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCumul                 = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       anchorResiduesPos                  = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    int*                         positions                          = new int [currentPredictBindingSiteByPattern->Sequence()->Length()]; 

    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++) {
      probabilitiesAA[i]    = currentPredictBindingSiteByPattern->ScoreAA()[i];
      probabilitiesCH[i]    = currentPredictBindingSiteByPattern->ScoreCH()[i];
      probabilitiesPH[i]    = currentPredictBindingSiteByPattern->ScorePH()[i];
      probabilitiesSS[i]    = currentPredictBindingSiteByPattern->ScoreSS()[i];
      probabilitiesCumul[i] = currentPredictBindingSiteByPattern->ScoreCumul()[i];
      anchorResiduesPos[i]  = currentPredictBindingSiteByPattern->AnchorResidues()[i];
    }

    char* parts     = strtok(currentPredictBindingSiteByPattern->Header()->Out(), "[;]");
    int   countPart = 0;
    int   counter   = 0;
    
    while (parts) {
      if (countPart++ > 0) {
        int start, end;
        sscanf(parts, "%d-%d", &start, &end);
        for (int i = start; i <= end; i++)
          if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
            positions[counter++] = i;
      }
      parts = strtok(NULL, "[;]");
    }

    if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
      for (int i = counter; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++, counter++) 
        positions[counter] = positions[counter - 1] + 1;
    
    filenameOut->In(this->folderOut);
    if (filenameOut->Out()[filenameOut->Length() - 1] != '/')
      filenameOut->Add("/");
    strncpy(buffer, currentPredictBindingSiteByPattern->Header()->Out() + 1, 7);
    buffer[7] = '\0';
    filenameOut->Add(buffer);
    filenameOut->Add(".Binding.Site.Prediction.Join");
    gnuplot_saveplotPNG(plot, filenameOut->Out(), 7, 700, 300);

    cmd->In("Ligand: ");
    cmd->Add(this->title->Out());
    cmd->Add(" - CATH Domain: ");
    cmd->Add(buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 1\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set label \"%s\" at screen 0.5,0.965 center front font \"FreeMono-Bold, 8\" tc rgb \"black\"\0", cmd->Out());
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set multiplot\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style line 1 lt -1 lc rgb \"#E69F17\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 2 lt -1 lc rgb \"#11AD34\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 3 lt -1 lc rgb \"#1D4599\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 4 lt -1 lc rgb \"#2F3F60\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 5 lt -1 lc rgb \"#666666\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 6 lt -1 lc rgb \"red\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.85\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.15\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_xlabel(plot, "Amino acid position in the sequence", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 

    cmd->In("set xtics (");
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i = i + 10) {
      sprintf(buffer, ((!i)?"'%d' %d":", '%d' %d"), positions[i], i);
      cmd->Add(buffer);
    }
    cmd->Add(") \0");
    gnuplot_cmd(plot, cmd->Out());
    sprintf(buffer, "set xtics rotate by 270 font \"FreeMono-Bold 6\"\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    
    gnuplot_setstyle(plot, "impulses ls 6");
    sprintf(buffer, "Anchor residues\0");
    gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_setstyle(plot, "linespoints ls 1");
    sprintf(buffer, "Identification by amino acid patterns\0");
    gnuplot_replot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_setstyle(plot, "linespoints ls 2");
    sprintf(buffer, "Identification by chemical patterns\0");
    gnuplot_replot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
     
    gnuplot_setstyle(plot, "linespoints ls 3");
    sprintf(buffer, "Identification by physical patterns\0");
    gnuplot_replot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    gnuplot_setstyle(plot, "linespoints ls 4");
    sprintf(buffer, "Identification by secondary structure patterns (predicted)\0");
    gnuplot_replot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    sprintf(buffer, "unset multiplot\0");
    gnuplot_cmd(plot, buffer);

    gnuplot_close(plot);

    if (probabilitiesAA)    delete [] probabilitiesAA;
    if (probabilitiesCH)    delete [] probabilitiesCH;
    if (probabilitiesPH)    delete [] probabilitiesPH;
    if (probabilitiesSS)    delete [] probabilitiesSS;
    if (probabilitiesCumul) delete [] probabilitiesCumul;
    if (anchorResiduesPos)  delete [] anchorResiduesPos;
    if (positions)          delete [] positions;

  }
  
  if (filenameOut) delete filenameOut;
  if (cmd)         delete cmd;

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPredictBindingSite::PlotAllInOnePS (void) {

  if (!this->listPredictionBS || !this->listPredictionBS->Length() || 
      !this->folderOut        || !this->folderOut->Length()        ||
      !this->title            || !this->title->Length())
    return false;

  char          buffer [MAX_SIZE];
  String*       filenameOut = new String ();
  String*       cmd         = new String ();
  
  this->listPredictionBS->SetInitial();
  while (this->listPredictionBS->SetNext()) {
    
    PredictBindingSiteByPattern* currentPredictBindingSiteByPattern = this->listPredictionBS->GetCurrent();

    gnuplot_ctrl*                plot                               = gnuplot_init();
    
    float*                       probabilitiesAA                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesPH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesSS                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCumul                 = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       anchorResiduesPos                  = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    int*                         positions                          = new int [currentPredictBindingSiteByPattern->Sequence()->Length()]; 

    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++) {
      probabilitiesAA[i]    = currentPredictBindingSiteByPattern->ScoreAA()[i];
      probabilitiesCH[i]    = currentPredictBindingSiteByPattern->ScoreCH()[i];
      probabilitiesPH[i]    = currentPredictBindingSiteByPattern->ScorePH()[i];
      probabilitiesSS[i]    = currentPredictBindingSiteByPattern->ScoreSS()[i];
      probabilitiesCumul[i] = currentPredictBindingSiteByPattern->ScoreCumul()[i];
      anchorResiduesPos[i]  = currentPredictBindingSiteByPattern->AnchorResidues()[i];
    }

    char* parts     = strtok(currentPredictBindingSiteByPattern->Header()->Out(), "[;]");
    int   countPart = 0;
    int   counter   = 0;
    
    while (parts) {
      if (countPart++ > 0) {
        int start, end;
        sscanf(parts, "%d-%d", &start, &end);
        for (int i = start; i <= end; i++)
          if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
            positions[counter++] = i;
      }
      parts = strtok(NULL, "[;]");
    }

    if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
      for (int i = counter; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++, counter++) 
        positions[counter] = positions[counter - 1] + 1;
    
    filenameOut->In(this->folderOut);
    if (filenameOut->Out()[filenameOut->Length() - 1] != '/')
      filenameOut->Add("/");
    strncpy(buffer, currentPredictBindingSiteByPattern->Header()->Out() + 1, 7);
    buffer[7] = '\0';
    filenameOut->Add(buffer);
    filenameOut->Add(".Binding.Site.Prediction.Join");
    gnuplot_saveplotPS(plot, filenameOut->Out(), 7, 700, 320);

    cmd->In("Ligand: ");
    cmd->Add(this->title->Out());
    cmd->Add(" - CATH Domain: ");
    cmd->Add(buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 1\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set label \"%s\" at screen 0.5,0.965 center front font \"FreeMono, 10\" tc rgb \"black\"\0", cmd->Out());
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set multiplot\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style line 1 lt -1 lc rgb \"#E69F17\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 2 lt -1 lc rgb \"#11AD34\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 3 lt -1 lc rgb \"#1D4599\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 4 lt -1 lc rgb \"#2F3F60\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 5 lt -1 lc rgb \"#666666\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 6 lt -1 lc rgb \"red\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.82\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.15\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_xlabel(plot, "Amino acid position in the sequence", "font \"FreeMono, 8\" tc rgb \"black\"");
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono, 8\" tc rgb \"black\"");
    
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 

    cmd->In("set xtics (");
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i = i + 10) {
      sprintf(buffer, ((!i)?"'%d' %d":", '%d' %d"), positions[i], i);
      cmd->Add(buffer);
    }
    cmd->Add(") \0");
    gnuplot_cmd(plot, cmd->Out());
    sprintf(buffer, "set xtics rotate by 270 font \"FreeMono-Bold 6\"\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    
    gnuplot_setstyle(plot, "impulses ls 6");
    sprintf(buffer, "Anchor residues\0");
    gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_setstyle(plot, "linespoints ls 1");
    sprintf(buffer, "Identification by amino acid patterns\0");
    gnuplot_replot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_setstyle(plot, "linespoints ls 2");
    sprintf(buffer, "Identification by chemical patterns\0");
    gnuplot_replot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
     
    gnuplot_setstyle(plot, "linespoints ls 3");
    sprintf(buffer, "Identification by physical patterns\0");
    gnuplot_replot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    gnuplot_setstyle(plot, "linespoints ls 4");
    sprintf(buffer, "Identification by secondary structure patterns (predicted)\0");
    gnuplot_replot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    sprintf(buffer, "unset multiplot\0");
    gnuplot_cmd(plot, buffer);

    gnuplot_close(plot);

    if (probabilitiesAA)    delete [] probabilitiesAA;
    if (probabilitiesCH)    delete [] probabilitiesCH;
    if (probabilitiesPH)    delete [] probabilitiesPH;
    if (probabilitiesSS)    delete [] probabilitiesSS;
    if (probabilitiesCumul) delete [] probabilitiesCumul;
    if (anchorResiduesPos)  delete [] anchorResiduesPos;
    if (positions)          delete [] positions;

  }
  
  if (filenameOut) delete filenameOut;
  if (cmd)         delete cmd;

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPredictBindingSite::PlotWeightedOneByOne (void) {

  if (!this->listPredictionBS || !this->listPredictionBS->Length() || 
      !this->folderOut        || !this->folderOut->Length()        ||
      !this->title            || !this->title->Length())
    return false;

  char          buffer [MAX_SIZE];
  String*       filenameOut = new String ();
  String*       cmd         = new String ();
  
  this->listPredictionBS->SetInitial();
  while (this->listPredictionBS->SetNext()) {
    
    PredictBindingSiteByPattern* currentPredictBindingSiteByPattern = this->listPredictionBS->GetCurrent();
    
    gnuplot_ctrl*                plot                               = gnuplot_init();
    
    float*                       probabilitiesAA                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesPH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesSS                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCumul                 = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       anchorResiduesPos                  = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    int*                         positions                          = new int [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
      
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++) {
      probabilitiesAA[i]    = currentPredictBindingSiteByPattern->ScoreAA()[i];
      probabilitiesCH[i]    = currentPredictBindingSiteByPattern->ScoreCH()[i];
      probabilitiesPH[i]    = currentPredictBindingSiteByPattern->ScorePH()[i];
      probabilitiesSS[i]    = currentPredictBindingSiteByPattern->ScoreSS()[i];
      probabilitiesCumul[i] = currentPredictBindingSiteByPattern->ScoreCumul()[i];
      anchorResiduesPos[i]  = currentPredictBindingSiteByPattern->AnchorResidues()[i];
    }
    
    char* parts     = strtok(currentPredictBindingSiteByPattern->Header()->Out(), "[;]");
    int   countPart = 0;
    int   counter   = 0;
    
    while (parts) {
      if (countPart++ > 0) {
        int start, end;
        sscanf(parts, "%d-%d", &start, &end);
        for (int i = start; i <= end; i++)
          if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
            positions[counter++] = i;
      }
      parts = strtok(NULL, "[;]");
    }
    
    if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
      for (int i = counter; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++, counter++) 
        positions[counter] = positions[counter - 1] + 1;
    
    filenameOut->In(this->folderOut);
    if (filenameOut->Out()[filenameOut->Length() - 1] != '/')
      filenameOut->Add("/");
    strncpy(buffer, currentPredictBindingSiteByPattern->Header()->Out() + 1, 7);
    buffer[7] = '\0';
    filenameOut->Add(buffer);
    filenameOut->Add(".Binding.Site.Prediction");
    gnuplot_saveplotPNG(plot, filenameOut->Out(), 7, 600, 600);
    
    cmd->In("Ligand: ");
    cmd->Add(this->title->Out());
    cmd->Add(" - CATH Domain: ");
    cmd->Add(buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 1\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set label \"%s\" at screen 0.5,0.99 center front font \"FreeMono-Bold, 8\" tc rgb \"black\"\0", cmd->Out());
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set multiplot\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style line 1 lt -1 lc rgb \"#E69F17\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 2 lt -1 lc rgb \"#11AD34\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 3 lt -1 lc rgb \"#1D4599\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 4 lt -1 lc rgb \"#2F3F60\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 5 lt -1 lc rgb \"#666666\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 6 lt -1 lc rgb \"red\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.795\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format y \"%%%%+-02.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 1":"points ls 1");
    sprintf(buffer, "Identification by amino acid patterns\0");
    if (SHOW_ANCHOR_RESIDUES) 
      gnuplot_replot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.615\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format y \"%%%%+-02.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 2":"points ls 2");
    sprintf(buffer, "Identification by chemical patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
     
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.435\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format y \"%%%%+-02.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 3":"points ls 3");
    sprintf(buffer, "Identification by physical patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.250\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format y \"%%%%+-02.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 4":"points ls 4");
    sprintf(buffer, "Identification by secondary structure patterns (predicted)\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 0.22\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.032\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set tmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set format y \"%%%%+-02.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set xtics 0, 10, %ld\0", currentPredictBindingSiteByPattern->Sequence()->Length());
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    cmd->In("set xtics (");
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i = i + 10) {
      sprintf(buffer, ((!i)?"'%d' %d":", '%d' %d"), positions[i], i);
      cmd->Add(buffer);
    }
    cmd->Add(") \0");
    gnuplot_cmd(plot, cmd->Out());
    sprintf(buffer, "set xtics rotate by 270 font \"FreeMono-Bold 6\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    
    gnuplot_setstyle(plot, "impulses ls 6");
    sprintf(buffer, "Anchor residues\0");
    gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_set_xlabel(plot, "Amino acid position in the sequence", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_setstyle(plot, (USE_IMPULSE_AVG)?"impulses ls 5":"points ls 5");
    sprintf(buffer, "Identification by patterns consensus\0");
    gnuplot_replot_x(plot, probabilitiesCumul, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset multiplot\0");
    gnuplot_cmd(plot, buffer);

    gnuplot_close(plot);

    if (probabilitiesAA)    delete [] probabilitiesAA;
    if (probabilitiesCH)    delete [] probabilitiesCH;
    if (probabilitiesPH)    delete [] probabilitiesPH;
    if (probabilitiesSS)    delete [] probabilitiesSS;
    if (probabilitiesCumul) delete [] probabilitiesCumul;
    if (anchorResiduesPos)  delete [] anchorResiduesPos;
    if (positions)          delete [] positions;
    
  }
  
  if (filenameOut) delete filenameOut;
  if (cmd)         delete cmd;
  
  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPredictBindingSite::PlotWeightedOneByOnePS (void) {

  if (!this->listPredictionBS || !this->listPredictionBS->Length() || 
      !this->folderOut        || !this->folderOut->Length()        ||
      !this->title            || !this->title->Length())
    return false;

  char          buffer [MAX_SIZE];
  String*       filenameOut = new String ();
  String*       cmd         = new String ();
  
  this->listPredictionBS->SetInitial();
  while (this->listPredictionBS->SetNext()) {
    
    PredictBindingSiteByPattern* currentPredictBindingSiteByPattern = this->listPredictionBS->GetCurrent();
    
    gnuplot_ctrl*                plot                               = gnuplot_init();
    
    float*                       probabilitiesAA                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesPH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesSS                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCumul                 = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       anchorResiduesPos                  = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    int*                         positions                          = new int [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
      
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++) {
      probabilitiesAA[i]    = currentPredictBindingSiteByPattern->ScoreAA()[i];
      probabilitiesCH[i]    = currentPredictBindingSiteByPattern->ScoreCH()[i];
      probabilitiesPH[i]    = currentPredictBindingSiteByPattern->ScorePH()[i];
      probabilitiesSS[i]    = currentPredictBindingSiteByPattern->ScoreSS()[i];
      probabilitiesCumul[i] = currentPredictBindingSiteByPattern->ScoreCumul()[i];
      anchorResiduesPos[i]  = currentPredictBindingSiteByPattern->AnchorResidues()[i];
    }
    
    char* parts     = strtok(currentPredictBindingSiteByPattern->Header()->Out(), "[;]");
    int   countPart = 0;
    int   counter   = 0;
    
    while (parts) {
      if (countPart++ > 0) {
        int start, end;
        sscanf(parts, "%d-%d", &start, &end);
        for (int i = start; i <= end; i++)
          if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
            positions[counter++] = i;
      }
      parts = strtok(NULL, "[;]");
    }
    
    if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
      for (int i = counter; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++, counter++) 
        positions[counter] = positions[counter - 1] + 1;
    
    filenameOut->In(this->folderOut);
    if (filenameOut->Out()[filenameOut->Length() - 1] != '/')
      filenameOut->Add("/");
    strncpy(buffer, currentPredictBindingSiteByPattern->Header()->Out() + 1, 7);
    buffer[7] = '\0';
    filenameOut->Add(buffer);
    filenameOut->Add(".Binding.Site.Prediction");
    gnuplot_saveplotPS(plot, filenameOut->Out(), 7, 600, 600);
    
    cmd->In("Ligand: ");
    cmd->Add(this->title->Out());
    cmd->Add(" - CATH Domain: ");
    cmd->Add(buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 1\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set label \"%s\" at screen 0.5,0.97 center front font \"FreeMono-Bold, 10\" tc rgb \"black\"\0", cmd->Out());
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set multiplot\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style line 1 lt -1 lc rgb \"#E69F17\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 2 lt -1 lc rgb \"#11AD34\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 3 lt -1 lc rgb \"#1D4599\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 4 lt -1 lc rgb \"#2F3F60\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 5 lt -1 lc rgb \"#666666\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 6 lt -1 lc rgb \"red\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.776\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format y \"%%%%+-2.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 1":"points ls 1");
    sprintf(buffer, "Identification by amino acid patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.604\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format y \"%%%%+-2.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 2":"points ls 2");
    sprintf(buffer, "Identification by chemical patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
     
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.432\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format y \"%%%%+-2.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 3":"points ls 3");
    sprintf(buffer, "Identification by physical patterns\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.260\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format y \"%%%%+-2.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set format x \"\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "unset xtics\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_ANCHOR_RESIDUES) {
      gnuplot_setstyle(plot, "impulses ls 6");
      sprintf(buffer, "Anchor residues\0");
      gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    }

    gnuplot_setstyle(plot, (USE_IMPULSE)?"impulses ls 4":"points ls 4");
    sprintf(buffer, "Identification by secondary structure patterns (predicted)\0");
    if (SHOW_ANCHOR_RESIDUES)
      gnuplot_replot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    else
      gnuplot_plot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 0.20\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.060\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set format y \"%%%%+-2.1e\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set tmarg 0\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set xtics 0, 10, %ld\0", currentPredictBindingSiteByPattern->Sequence()->Length());
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 
    cmd->In("set xtics (");
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i = i + 10) {
      sprintf(buffer, ((!i)?"'%d' %d":", '%d' %d"), positions[i], i);
      cmd->Add(buffer);
    }
    cmd->Add(") \0");
    gnuplot_cmd(plot, cmd->Out());
    sprintf(buffer, "set xtics rotate by 270 font \"FreeMono-Bold 6\"\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0"); 
    gnuplot_cmd(plot, buffer);
    
    gnuplot_setstyle(plot, "impulses ls 6");
    sprintf(buffer, "Anchor residues\0");
    gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_set_xlabel(plot, "Amino acid position in the sequence", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_setstyle(plot, (USE_IMPULSE_AVG)?"impulses ls 5":"points ls 5");
    sprintf(buffer, "Identification by patterns consensus\0");
    gnuplot_replot_x(plot, probabilitiesCumul, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    sprintf(buffer, "unset multiplot\0");
    gnuplot_cmd(plot, buffer);

    gnuplot_close(plot);

    if (probabilitiesAA)    delete [] probabilitiesAA;
    if (probabilitiesCH)    delete [] probabilitiesCH;
    if (probabilitiesPH)    delete [] probabilitiesPH;
    if (probabilitiesSS)    delete [] probabilitiesSS;
    if (probabilitiesCumul) delete [] probabilitiesCumul;
    if (anchorResiduesPos)  delete [] anchorResiduesPos;
    if (positions)          delete [] positions;
    
  }
  
  if (filenameOut) delete filenameOut;
  if (cmd)         delete cmd;
  
  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPredictBindingSite::PlotWeightedAllInOne (void) {

  if (!this->listPredictionBS || !this->listPredictionBS->Length() || 
      !this->folderOut        || !this->folderOut->Length()        ||
      !this->title            || !this->title->Length())
    return false;

  char          buffer [MAX_SIZE];
  String*       filenameOut = new String ();
  String*       cmd         = new String ();
  
  this->listPredictionBS->SetInitial();
  while (this->listPredictionBS->SetNext()) {
    
    PredictBindingSiteByPattern* currentPredictBindingSiteByPattern = this->listPredictionBS->GetCurrent();

    gnuplot_ctrl*                plot                               = gnuplot_init();
    
    float*                       probabilitiesAA                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesPH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesSS                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCumul                 = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       anchorResiduesPos                  = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    int*                         positions                          = new int [currentPredictBindingSiteByPattern->Sequence()->Length()]; 

    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++) {
      probabilitiesAA[i]    = currentPredictBindingSiteByPattern->ScoreAA()[i];
      probabilitiesCH[i]    = currentPredictBindingSiteByPattern->ScoreCH()[i];
      probabilitiesPH[i]    = currentPredictBindingSiteByPattern->ScorePH()[i];
      probabilitiesSS[i]    = currentPredictBindingSiteByPattern->ScoreSS()[i];
      probabilitiesCumul[i] = currentPredictBindingSiteByPattern->ScoreCumul()[i];
      anchorResiduesPos[i]  = currentPredictBindingSiteByPattern->AnchorResidues()[i];
    }

    char* parts     = strtok(currentPredictBindingSiteByPattern->Header()->Out(), "[;]");
    int   countPart = 0;
    int   counter   = 0;
    
    while (parts) {
      if (countPart++ > 0) {
        int start, end;
        sscanf(parts, "%d-%d", &start, &end);
        for (int i = start; i <= end; i++)
          if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
            positions[counter++] = i;
      }
      parts = strtok(NULL, "[;]");
    }

    if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
      for (int i = counter; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++, counter++) 
        positions[counter] = positions[counter - 1] + 1;
    
    filenameOut->In(this->folderOut);
    if (filenameOut->Out()[filenameOut->Length() - 1] != '/')
      filenameOut->Add("/");
    strncpy(buffer, currentPredictBindingSiteByPattern->Header()->Out() + 1, 7);
    buffer[7] = '\0';
    filenameOut->Add(buffer);
    filenameOut->Add(".Binding.Site.Prediction.Join");
    gnuplot_saveplotPNG(plot, filenameOut->Out(), 7, 700, 300);

    cmd->In("Ligand: ");
    cmd->Add(this->title->Out());
    cmd->Add(" - CATH Domain: ");
    cmd->Add(buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 1\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set label \"%s\" at screen 0.5,0.965 center front font \"FreeMono-Bold, 8\" tc rgb \"black\"\0", cmd->Out());
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set multiplot\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style line 1 lt -1 lc rgb \"#E69F17\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 2 lt -1 lc rgb \"#11AD34\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 3 lt -1 lc rgb \"#1D4599\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 4 lt -1 lc rgb \"#2F3F60\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 5 lt -1 lc rgb \"#666666\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 6 lt -1 lc rgb \"red\" lw 1 pt 7 ps 0.5\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.85\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.15\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_xlabel(plot, "Amino acid position in the sequence", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono-Bold, 8\" tc rgb \"black\"");
    
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 

    cmd->In("set xtics (");
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i = i + 10) {
      sprintf(buffer, ((!i)?"'%d' %d":", '%d' %d"), positions[i], i);
      cmd->Add(buffer);
    }
    cmd->Add(") \0");
    gnuplot_cmd(plot, cmd->Out());
    sprintf(buffer, "set xtics rotate by 270 font \"FreeMono-Bold 6\"\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    
    gnuplot_setstyle(plot, "impulses ls 6");
    sprintf(buffer, "Anchor residues\0");
    gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_setstyle(plot, "linespoints ls 1");
    sprintf(buffer, "Identification by amino acid patterns\0");
    gnuplot_replot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_setstyle(plot, "linespoints ls 2");
    sprintf(buffer, "Identification by chemical patterns\0");
    gnuplot_replot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
     
    gnuplot_setstyle(plot, "linespoints ls 3");
    sprintf(buffer, "Identification by physical patterns\0");
    gnuplot_replot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    gnuplot_setstyle(plot, "linespoints ls 4");
    sprintf(buffer, "Identification by secondary structure patterns (predicted)\0");
    gnuplot_replot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    sprintf(buffer, "unset multiplot\0");
    gnuplot_cmd(plot, buffer);

    gnuplot_close(plot);

    if (probabilitiesAA)    delete [] probabilitiesAA;
    if (probabilitiesCH)    delete [] probabilitiesCH;
    if (probabilitiesPH)    delete [] probabilitiesPH;
    if (probabilitiesSS)    delete [] probabilitiesSS;
    if (probabilitiesCumul) delete [] probabilitiesCumul;
    if (anchorResiduesPos)  delete [] anchorResiduesPos;
    if (positions)          delete [] positions;

  }
  
  if (filenameOut) delete filenameOut;
  if (cmd)         delete cmd;

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PlotPredictBindingSite::PlotWeightedAllInOnePS (void) {

  if (!this->listPredictionBS || !this->listPredictionBS->Length() || 
      !this->folderOut        || !this->folderOut->Length()        ||
      !this->title            || !this->title->Length())
    return false;

  char          buffer [MAX_SIZE];
  String*       filenameOut = new String ();
  String*       cmd         = new String ();
  
  this->listPredictionBS->SetInitial();
  while (this->listPredictionBS->SetNext()) {
    
    PredictBindingSiteByPattern* currentPredictBindingSiteByPattern = this->listPredictionBS->GetCurrent();

    gnuplot_ctrl*                plot                               = gnuplot_init();
    
    float*                       probabilitiesAA                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesPH                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesSS                    = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       probabilitiesCumul                 = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    float*                       anchorResiduesPos                  = new float [currentPredictBindingSiteByPattern->Sequence()->Length()]; 
    int*                         positions                          = new int [currentPredictBindingSiteByPattern->Sequence()->Length()]; 

    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++) {
      probabilitiesAA[i]    = currentPredictBindingSiteByPattern->ScoreAA()[i];
      probabilitiesCH[i]    = currentPredictBindingSiteByPattern->ScoreCH()[i];
      probabilitiesPH[i]    = currentPredictBindingSiteByPattern->ScorePH()[i];
      probabilitiesSS[i]    = currentPredictBindingSiteByPattern->ScoreSS()[i];
      probabilitiesCumul[i] = currentPredictBindingSiteByPattern->ScoreCumul()[i];
      anchorResiduesPos[i]  = currentPredictBindingSiteByPattern->AnchorResidues()[i];
    }

    char* parts     = strtok(currentPredictBindingSiteByPattern->Header()->Out(), "[;]");
    int   countPart = 0;
    int   counter   = 0;
    
    while (parts) {
      if (countPart++ > 0) {
        int start, end;
        sscanf(parts, "%d-%d", &start, &end);
        for (int i = start; i <= end; i++)
          if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
            positions[counter++] = i;
      }
      parts = strtok(NULL, "[;]");
    }

    if (counter < currentPredictBindingSiteByPattern->Sequence()->Length())
      for (int i = counter; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++, counter++) 
        positions[counter] = positions[counter - 1] + 1;
    
    filenameOut->In(this->folderOut);
    if (filenameOut->Out()[filenameOut->Length() - 1] != '/')
      filenameOut->Add("/");
    strncpy(buffer, currentPredictBindingSiteByPattern->Header()->Out() + 1, 7);
    buffer[7] = '\0';
    filenameOut->Add(buffer);
    filenameOut->Add(".Binding.Site.Prediction.Join");
    gnuplot_saveplotPS(plot, filenameOut->Out(), 7, 700, 320);

    cmd->In("Ligand: ");
    cmd->Add(this->title->Out());
    cmd->Add(" - CATH Domain: ");
    cmd->Add(buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set size 1, 1\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set label \"%s\" at screen 0.5,0.965 center front font \"FreeMono, 10\" tc rgb \"black\"\0", cmd->Out());
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set multiplot\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style line 1 lt -1 lc rgb \"#E69F17\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 2 lt -1 lc rgb \"#11AD34\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 3 lt -1 lc rgb \"#1D4599\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 4 lt -1 lc rgb \"#2F3F60\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 5 lt -1 lc rgb \"#666666\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set style line 6 lt -1 lc rgb \"red\" lw 1 pt 7 ps 0.3\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "unset key\0");
    gnuplot_cmd(plot, buffer);
    
    sprintf(buffer, "set size 1, 0.82\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set origin 0.0, 0.15\0");
    gnuplot_cmd(plot, buffer);
    if (SHOW_Y_TICS) {
      sprintf(buffer, "set grid ytics\0");
      gnuplot_cmd(plot, buffer);
    }
    sprintf(buffer, "set tmarg 2\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set bmarg 0\0");
    gnuplot_cmd(plot, buffer);
    if (AUTO_SCALE_WEIGHTED) {
      sprintf(buffer, "set autoscale y\0");
      gnuplot_cmd(plot, buffer);
    }
    else {
      gnuplot_set_yrange(plot, 0, 1); 
      sprintf(buffer, "set ytics (0.2, 0.4, 0.6, 0.8, 1)\0");
      gnuplot_cmd(plot, buffer);
    }
    gnuplot_set_xlabel(plot, "Amino acid position in the sequence", "font \"FreeMono, 8\" tc rgb \"black\"");
    gnuplot_set_ylabel(plot, "Score", "font \"FreeMono, 8\" tc rgb \"black\"");
    
    gnuplot_set_xrange(plot, 0, currentPredictBindingSiteByPattern->Sequence()->Length()); 

    cmd->In("set xtics (");
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i = i + 10) {
      sprintf(buffer, ((!i)?"'%d' %d":", '%d' %d"), positions[i], i);
      cmd->Add(buffer);
    }
    cmd->Add(") \0");
    gnuplot_cmd(plot, cmd->Out());
    sprintf(buffer, "set xtics rotate by 270 font \"FreeMono-Bold 6\"\0");
    gnuplot_cmd(plot, buffer);

    sprintf(buffer, "set style fill solid\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key right top\0");
    gnuplot_cmd(plot, buffer);
    sprintf(buffer, "set key tc rgb \"black\"\0");
    gnuplot_cmd(plot, buffer);
    
    gnuplot_setstyle(plot, "impulses ls 6");
    sprintf(buffer, "Anchor residues\0");
    gnuplot_plot_x(plot, anchorResiduesPos, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_setstyle(plot, "linespoints ls 1");
    sprintf(buffer, "Identification by amino acid patterns\0");
    gnuplot_replot_x(plot, probabilitiesAA, currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    gnuplot_setstyle(plot, "linespoints ls 2");
    sprintf(buffer, "Identification by chemical patterns\0");
    gnuplot_replot_x(plot, probabilitiesCH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
     
    gnuplot_setstyle(plot, "linespoints ls 3");
    sprintf(buffer, "Identification by physical patterns\0");
    gnuplot_replot_x(plot, probabilitiesPH,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   
    
    gnuplot_setstyle(plot, "linespoints ls 4");
    sprintf(buffer, "Identification by secondary structure patterns (predicted)\0");
    gnuplot_replot_x(plot, probabilitiesSS,    currentPredictBindingSiteByPattern->Sequence()->Length(), buffer);   

    sprintf(buffer, "unset multiplot\0");
    gnuplot_cmd(plot, buffer);

    gnuplot_close(plot);

    if (probabilitiesAA)    delete [] probabilitiesAA;
    if (probabilitiesCH)    delete [] probabilitiesCH;
    if (probabilitiesPH)    delete [] probabilitiesPH;
    if (probabilitiesSS)    delete [] probabilitiesSS;
    if (probabilitiesCumul) delete [] probabilitiesCumul;
    if (anchorResiduesPos)  delete [] anchorResiduesPos;
    if (positions)          delete [] positions;

  }
  
  if (filenameOut) delete filenameOut;
  if (cmd)         delete cmd;

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PlotPredictBindingSite PlotPredictBindingSite::operator= (const PlotPredictBindingSite& plotPredictBindingSite) {

  *(this->filenameIn)       = *(plotPredictBindingSite.filenameIn);
  *(this->folderOut)        = *(plotPredictBindingSite.folderOut);

  *(this->title)            = *(plotPredictBindingSite.title);
  *(this->listPredictionBS) = *(plotPredictBindingSite.listPredictionBS);

  *(this->toString)         = *(plotPredictBindingSite.toString);
  
  return *this;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
